{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9bafacaa-d8e8-4cf5-abcb-924961dc8731",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i love Python! <class 'str'>\n",
      "I love Python! <class 'str'>\n",
      "13\n",
      "58\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "字符串的定义¶\n",
    "Python 中字符串被定义为引号之间的字符集合。\n",
    "Python 支持使用成对的 单引号 或 双引号。\n",
    "'''\n",
    "\n",
    "t1 = 'i love Python!'\n",
    "print(t1, type(t1))\n",
    "# i love Python! <class 'str'>\n",
    "\n",
    "t2 = \"I love Python!\"\n",
    "print(t2, type(t2))\n",
    "# I love Python! <class 'str'>\n",
    "\n",
    "print(5 + 8)  # 13\n",
    "print('5' + '8')  # 58"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "35150640-5f36-4ab5-9106-3c2af11be72d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "let's go\n",
      "let's go\n",
      "C:\\now\n",
      "C:\\Program Files\\Intel\\Wifi\\Help\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串中需要出现单引号或双引号，可以使用转义符号\\对字符串中的符号进行转义。\n",
    "print('let\\'s go')  # let's go\n",
    "print(\"let's go\")  # let's go\n",
    "print('C:\\\\now')  # C:\\now\n",
    "print(\"C:\\\\Program Files\\\\Intel\\\\Wifi\\\\Help\")\n",
    "# C:\\Program Files\\Intel\\Wifi\\Help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0cdb7391-5360-4f45-94a6-b9722c536b15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Program Files\\Intel\\Wifi\\Help\n"
     ]
    }
   ],
   "source": [
    "#原始字符串只需要在字符串前边加一个英文字母 r 即可。不转义字符串的转义符\n",
    "print(r'C:\\Program Files\\Intel\\Wifi\\Help')  \n",
    "# C:\\Program Files\\Intel\\Wifi\\Help"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7606cb4c-01d5-40f1-9517-ab95dcc5ebab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个多行字符串的实例\n",
      "多行字符串可以使用制表符\n",
      "TAB ( \t )。\n",
      "也可以使用换行符 [ \n",
      " ]。\n",
      "\n",
      "这是一个多行字符串的实例\n",
      "多行字符串可以使用制表符\n",
      "TAB ( \t )。\n",
      "也可以使用换行符 [ \n",
      " ]。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "para_str = \"\"\"这是一个多行字符串的实例\n",
    "多行字符串可以使用制表符\n",
    "TAB ( \\t )。\n",
    "也可以使用换行符 [ \\n ]。\n",
    "\"\"\"\n",
    "print(para_str)\n",
    "# 这是一个多行字符串的实例\n",
    "# 多行字符串可以使用制表符\n",
    "# TAB (    )。\n",
    "# 也可以使用换行符 [\n",
    "#  ]。\n",
    "\n",
    "para_str = '''这是一个多行字符串的实例\n",
    "多行字符串可以使用制表符\n",
    "TAB ( \\t )。\n",
    "也可以使用换行符 [ \\n ]。\n",
    "'''\n",
    "print(para_str)\n",
    "# 这是一个多行字符串的实例\n",
    "# 多行字符串可以使用制表符\n",
    "# TAB ( \t )。\n",
    "# 也可以使用换行符 [ \n",
    "#  ]。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ad7463f9-7339-43a7-bf53-7b67b8dcce73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I Love\n",
      "e\n",
      "I Love 插入的字符串  LsgoGroup\n",
      "Python\n",
      "th\n",
      "yth\n",
      "t\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "str1 = 'I Love LsgoGroup'\n",
    "print(str1[:6])  # I Love\n",
    "print(str1[5])  # e\n",
    "print(str1[:6] + \" 插入的字符串 \" + str1[6:])  \n",
    "# I Love 插入的字符串  LsgoGroup\n",
    "\n",
    "s = 'Python'\n",
    "print(s)  # Python\n",
    "print(s[2:4])  # th\n",
    "print(s[-5:-2])  # yth\n",
    "print(s[2])  # t\n",
    "print(s[-1])  # n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6cb576cd-e931-4b57-8027-33a9d2757ffb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Xiaoxie\n",
      "daxiexiaoxie\n",
      "DAXIEXIAOXIE\n",
      "daxieXIAOXIE\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "capitalize() 将字符串的第一个字符转换为大写。\n",
    "lower() 转换字符串中所有大写字符为小写。\n",
    "upper() 转换字符串中的小写字母为大写。\n",
    "swapcase() 将字符串中大写转换为小写，小写转换为大写。\n",
    "'''\n",
    "str2 = 'xiaoxie'\n",
    "print(str2.capitalize())  # Xiaoxie\n",
    "\n",
    "str2 = \"DAXIExiaoxie\"\n",
    "print(str2.lower())  # daxiexiaoxie\n",
    "print(str2.upper())  # DAXIEXIAOXIE\n",
    "print(str2.swapcase())  # daxieXIAOXIE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "31f95d36-92ee-4bda-94ab-eab780ad8d8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "count(str, beg= 0,end=len(string)) 返回str在 string 里面出现的次数，如果beg或者end指定则返回指定范围内str出现的次数。\n",
    "'''\n",
    "str2 = \"DAXIExiaoxie\"\n",
    "print(str2.count('xi'))  # 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ce37f081-66b1-476f-bbb7-8ca5a104ea5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n",
      "False\n",
      "True\n",
      "5\n",
      "-1\n",
      "9\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "endswith(suffix, beg=0, end=len(string)) 检查字符串是否以指定子字符串 suffix 结束，如果是，返回 True，否则返回 False。如果 beg 和 end 指定值，则在指定范围内检查。\n",
    "startswith(substr, beg=0,end=len(string)) 检查字符串是否以指定子字符串 substr 开头，如果是，返回 True，否则返回 False。如果 beg 和 end 指定值，则在指定范围内检查。\n",
    "find(str, beg=0, end=len(string)) 检测 str 是否包含在字符串中，如果指定范围 beg 和 end，则检查是否包含在指定范围内，如果包含，返回开始的索引值，否则返回 -1。\n",
    "rfind(str, beg=0,end=len(string)) 类似于 find() 函数，不过是从右边开始查找。\n",
    "isnumeric() 如果字符串中只包含数字字符，则返回 True，否则返回 False。\n",
    "'''\n",
    "\n",
    "str2 = \"DAXIExiaoxie\"\n",
    "print(str2.endswith('ie'))  # True\n",
    "print(str2.endswith('xi'))  # False\n",
    "print(str2.startswith('Da'))  # False\n",
    "print(str2.startswith('DA'))  # True\n",
    "print(str2.find('xi'))  # 5\n",
    "print(str2.find('ix'))  # -1\n",
    "print(str2.rfind('xi'))  # 9\n",
    "str3 = '12345'\n",
    "print(str3.isnumeric())  # True\n",
    "str3 += 'a'\n",
    "print(str3.isnumeric())  # False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "51bb02c8-a11f-4eee-a40f-f261ac103ecd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11010000\n",
      "00001101\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "ljust(width[, fillchar])返回一个原字符串左对齐，并使用fillchar（默认空格）填充至长度width的新字符串。\n",
    "rjust(width[, fillchar])返回一个原字符串右对齐，并使用fillchar（默认空格）填充至长度width的新字符串。\n",
    "'''\n",
    "\n",
    "str4 = '1101'\n",
    "print(str4.ljust(8, '0'))  # 11010000\n",
    "print(str4.rjust(8, '0'))  # 00001101"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2aa5e7c7-b756-43a7-bf4b-92b03b51d51e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I Love LsgoGroup \n",
      " Love LsgoGroup \n",
      " I Love LsgoGroup\n",
      "I Love LsgoGroup\n",
      "I Love LsgoGrou\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "lstrip([chars]) 截掉字符串左边的空格或指定字符。\n",
    "rstrip([chars]) 删除字符串末尾的空格或指定字符。\n",
    "strip([chars]) 在字符串上执行lstrip()和rstrip()。\n",
    "'''\n",
    "\n",
    "str5 = ' I Love LsgoGroup '\n",
    "print(str5.lstrip())  # 'I Love LsgoGroup '\n",
    "print(str5.lstrip().strip('I'))  # ' Love LsgoGroup '\n",
    "print(str5.rstrip())  # ' I Love LsgoGroup'\n",
    "print(str5.strip())  # 'I Love LsgoGroup'\n",
    "print(str5.strip().strip('p'))  # 'I Love LsgoGrou'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "931f942b-b34c-4400-beda-422de18b4926",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('I L', 'o', 've LsgoGroup')\n",
      "('I Love LsgoGroup', '', '')\n",
      "('I Love LsgoGr', 'o', 'up')\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "partition(sub) 找到子字符串sub，把字符串分为一个三元组(pre_sub,sub,fol_sub)，如果字符串中不包含sub则返回('原字符串','','')。\n",
    "rpartition(sub)类似于partition()方法，不过是从右边开始查找。\n",
    "'''\n",
    "\n",
    "str5 = ' I Love LsgoGroup '\n",
    "print(str5.strip().partition('o'))  # ('I L', 'o', 've LsgoGroup')\n",
    "print(str5.strip().partition('m'))  # ('I Love LsgoGroup', '', '')\n",
    "print(str5.strip().rpartition('o'))  # ('I Love LsgoGr', 'o', 'up')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5067fd5f-8ed1-43c5-93ea-c7b9d00a8c53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We Love LsgoGroup\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "replace(old, new [, max]) 把 将字符串中的old替换成new，如果max指定，则替换不超过max次。\n",
    "'''\n",
    "str5 = ' I Love LsgoGroup '\n",
    "print(str5.strip().replace('I', 'We'))  # We Love LsgoGroup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "494cdb6c-aad6-49cc-ad81-fc9dd5b89803",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['I', 'Love', 'LsgoGroup']\n",
      "['I L', 've Lsg', 'Gr', 'up']\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "split(str=\"\", num) 不带参数默认是以空格为分隔符切片字符串，如果num参数有设置，则仅分隔num个子字符串，返回切片后的子字符串拼接的列表。\n",
    "'''\n",
    "str5 = ' I Love LsgoGroup '\n",
    "print(str5.strip().split())  # ['I', 'Love', 'LsgoGroup']\n",
    "print(str5.strip().split('o'))  # ['I L', 've Lsg', 'Gr', 'up']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "53e9b3e4-9b6c-4097-996c-5c9536a307bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['www.baidu.com.cn']\n",
      "['www', 'baidu', 'com', 'cn']\n",
      "['www.baidu.com.cn']\n",
      "['www', 'baidu.com.cn']\n",
      "['www', 'baidu', 'com.cn']\n",
      "baidu\n",
      "www\n",
      "baidu\n",
      "com.cn\n"
     ]
    }
   ],
   "source": [
    "u = \"www.baidu.com.cn\"\n",
    "# 使用默认分隔符\n",
    "print(u.split())  # ['www.baidu.com.cn']\n",
    "\n",
    "# 以\".\"为分隔符\n",
    "print((u.split('.')))  # ['www', 'baidu', 'com', 'cn']\n",
    "\n",
    "# 分割0次\n",
    "print((u.split(\".\", 0)))  # ['www.baidu.com.cn']\n",
    "\n",
    "# 分割一次\n",
    "print((u.split(\".\", 1)))  # ['www', 'baidu.com.cn']\n",
    "\n",
    "# 分割两次\n",
    "print(u.split(\".\", 2))  # ['www', 'baidu', 'com.cn']\n",
    "\n",
    "# 分割两次，并取序列为1的项\n",
    "print((u.split(\".\", 2)[1]))  # baidu\n",
    "\n",
    "# 分割两次，并把分割后的三个部分保存到三个变量\n",
    "u1, u2, u3 = u.split(\".\", 2)\n",
    "print(u1)  # www\n",
    "print(u2)  # baidu\n",
    "print(u3)  # com.cn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "bcd6f75b-83e8-4ddd-8d61-14fcfcbec3d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "say\n",
      "hello\n",
      "baby\n",
      "['say', 'hello', 'baby']\n"
     ]
    }
   ],
   "source": [
    "c = '''say\n",
    "hello\n",
    "baby'''\n",
    "\n",
    "print(c)\n",
    "# say\n",
    "# hello\n",
    "# baby\n",
    "\n",
    "print(c.split('\\n'))  # ['say', 'hello', 'baby']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ecdd05b3-bb56-45cc-a3f8-271f3e46f2ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "www.baidu.com\n",
      "['www', 'baidu', 'com']\n"
     ]
    }
   ],
   "source": [
    "string = \"hello boy<[www.baidu.com]>byebye\"\n",
    "print(string.split('[')[1].split(']')[0])  # www.baidu.com\n",
    "print(string.split('[')[1].split(']')[0].split('.'))  # ['www', 'baidu', 'com']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "92c289d6-6f33-4404-b806-d551e785eae5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['I ', ' Love ', ' LsgoGroup']\n",
      "['I \\n', ' Love \\n', ' LsgoGroup']\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "splitlines([keepends]) 按照行('\\r', '\\r\\n', \\n')分隔，返回一个包含各行作为元素的列表，如果参数keepends为 False，不包含换行符，如果为 True，则保留换行符。\n",
    "\"\"\"\n",
    "str6 = 'I \\n Love \\n LsgoGroup'\n",
    "print(str6.splitlines())  # ['I ', ' Love ', ' LsgoGroup']\n",
    "print(str6.splitlines(True))  # ['I \\n', ' Love \\n', ' LsgoGroup']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f75527e8-bb3f-4a1b-8ff7-9514dc987f48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{97: 49, 101: 50, 105: 51, 111: 52, 117: 53}\n",
      "{97: 49, 101: 50, 105: 51, 111: 52, 117: 53}\n",
      "th3s 3s str3ng 2x1mpl2....w4w!!!\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "maketrans(intab, outtab) 创建字符映射的转换表，第一个参数是字符串，表示需要转换的字符，第二个参数也是字符串表示转换的目标。\n",
    "translate(table, deletechars=\"\") 根据参数table给出的表，转换字符串的字符，要过滤掉的字符放到deletechars参数中。\n",
    "'''\n",
    "str7 = 'this is string example....wow!!!'\n",
    "intab = 'aeiou'\n",
    "outtab = '12345'\n",
    "trantab = ''.maketrans(intab, outtab)\n",
    "print(trantab)  # {97: 49, 101: 50, 105: 51, 111: 52, 117: 53}\n",
    "print('{%s: %s, %s: %s, %s: %s, %s: %s, %s: %s}' % (ord('a'), ord('1'), ord('e'), ord('2'), ord('i'), ord('3'), ord('o'), ord('4'), ord('u'), ord('5')))\n",
    "print(str7.translate(trantab))  # th3s 3s str3ng 2x1mpl2....w4w!!!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1f0d961f-9d6a-45f2-a76e-691e56d20bb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I Love Lsgogroup\n",
      "I Love Lsgogroup\n",
      "I Love Lsgogroup\n",
      "27.66GB\n",
      "a\n",
      "a b c\n",
      "4 + 5 = 9\n",
      "我叫 小明 今年 10 岁!\n",
      "12\n",
      "a\n",
      "A\n",
      "27.658000\n",
      "2.765800e+01\n",
      "2.765800E+01\n",
      "27.658\n",
      "I said: I am 22 years old..\n",
      "I said: 'I am 22 years old.'.\n"
     ]
    }
   ],
   "source": [
    "# format 格式化函数\n",
    "'''\n",
    "python 字符串格式化符号\n",
    "符 号\t描述\n",
    "%c\t格式化字符及其ASCII码\n",
    "%s\t格式化字符串，用str()方法处理对象\n",
    "%r\t格式化字符串，用rper()方法处理对象\n",
    "%d\t格式化整数\n",
    "%o\t格式化无符号八进制数\n",
    "%x\t格式化无符号十六进制数\n",
    "%X\t格式化无符号十六进制数（大写）\n",
    "%f\t格式化浮点数字，可指定小数点后的精度\n",
    "%e\t用科学计数法格式化浮点数\n",
    "%E\t作用同%e，用科学计数法格式化浮点数\n",
    "%g\t根据值的大小决定使用%f或%e\n",
    "%G\t作用同%g，根据值的大小决定使用%f或%E\n",
    "'''\n",
    "str8 = \"{0} Love {1}\".format('I', 'Lsgogroup')  # 位置参数\n",
    "print(str8)  # I Love Lsgogroup\n",
    "\n",
    "str8 = \"{a} Love {b}\".format(a='I', b='Lsgogroup')  # 关键字参数\n",
    "print(str8)  # I Love Lsgogroup\n",
    "\n",
    "str8 = \"{0} Love {b}\".format('I', b='Lsgogroup')  # 位置参数要在关键字参数之前\n",
    "print(str8)  # I Love Lsgogroup\n",
    "\n",
    "str8 = '{0:.2f}{1}'.format(27.658, 'GB')  # 保留小数点后两位\n",
    "print(str8)  # 27.66GB\n",
    "\n",
    "print('%c' % 97)  # a\n",
    "print('%c %c %c' % (97, 98, 99))  # a b c\n",
    "print('%d + %d = %d' % (4, 5, 9))  # 4 + 5 = 9\n",
    "print(\"我叫 %s 今年 %d 岁!\" % ('小明', 10))  # 我叫 小明 今年 10 岁!\n",
    "print('%o' % 10)  # 12\n",
    "print('%x' % 10)  # a\n",
    "print('%X' % 10)  # A\n",
    "print('%f' % 27.658)  # 27.658000\n",
    "print('%e' % 27.658)  # 2.765800e+01\n",
    "print('%E' % 27.658)  # 2.765800E+01\n",
    "print('%g' % 27.658)  # 27.658\n",
    "text = \"I am %d years old.\" % 22\n",
    "print(\"I said: %s.\" % text)  # I said: I am 22 years old..\n",
    "print(\"I said: %r.\" % text)  # I said: 'I am 22 years old.'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81e40499-7f02-475b-9c4b-c644e3a747c0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
